package socketing;

import java.io.*;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
//短链接实例　ｈｔｔｐ
public class V3ClientHttp {
    public static final ExecutorService service = Executors.newFixedThreadPool(4);
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (true){
            String s = scanner.nextLine();
            RequestConfig req = new RequestConfig();
            req.setHost("master");
            req.setPort(6666);
            req.setData(s);
            req.setPath("/some");
            req.setSuccess(res -> {
                System.out.println(res);
            });
            ajax(req);
        }
    }
    public static void ajax(RequestConfig requestConfig){
        Tool.debug("本线程制作完请求即将要提交请求");
        service.submit(new SocketMission(requestConfig,new ResponceConfig()));

    }

}
class SocketMission implements Runnable{

    private Socket socket;

    private RequestConfig req;
    private ResponceConfig res;

    public SocketMission( RequestConfig requestConfig, ResponceConfig responceConfig) {
        this.req = requestConfig;
        this.res = responceConfig;
    }


    public Socket getSocket() {
        return socket;
    }

    public void setSocket(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try {
            Tool.debug("由本线程为您发送请求和接收回复"+req);
//            socket 复用问题　多次ｈｔｔｐ请求的复用
            socket = new Socket(req.getHost(), req.getPort());
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(
                            socket.getOutputStream(), StandardCharsets.UTF_8));
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(
                            socket.getInputStream(), StandardCharsets.UTF_8));
            writer.write(req.getPath()+":"+req.getData());
            writer.newLine();
            writer.flush();
            Tool.debug("要阻塞----，神奇的等我通知必然要消耗线程");
            String s = reader.readLine();
            String[] split = s.split(":");
            res.setStatus(split[0]);
            res.setBody(split[1]);
            Tool.debug("哪个线程制作的请求？哪个线程运行的成功函数？");
            req.getSuccess().accept(res);
            reader.close();
            writer.close();
            socket.close();

        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
class RequestConfig{
    String host;
    int port;
    String path;
    String data;
    Consumer<ResponceConfig> success;

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public int getPort() {
        return port;
    }

    public void setPort(int port) {
        this.port = port;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public Consumer<ResponceConfig> getSuccess() {
        return success;
    }

    public void setSuccess(Consumer<ResponceConfig> success) {
        this.success = success;
    }

    @Override
    public String toString() {
        return "RequestConfig{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", path='" + path + '\'' +
                ", data='" + data + '\'' +
                '}';
    }
}
class ResponceConfig{
    String status;
    String body;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getBody() {
        return body;
    }

    public void setBody(String body) {
        this.body = body;
    }

    @Override
    public String toString() {
        return "ResponceConfig{" +
                "status='" + status + '\'' +
                ", body='" + body + '\'' +
                '}';
    }
}
